package com.jpyl.jpMedicalTreatment.view.activity.self;

import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.forxindu.kit.box.ViewKit;
import com.google.gson.JsonObject;
import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.model.ErrorMessageModel;
import com.jpyl.jpMedicalTreatment.model.ResponseModel;
import com.jpyl.jpMedicalTreatment.model.RxSpf_UserModel;
import com.jpyl.jpMedicalTreatment.network.Network;
import com.jpyl.jpMedicalTreatment.network.api.ApiError;
import com.jpyl.jpMedicalTreatment.network.api.RequestApi;
import com.jpyl.jpMedicalTreatment.util.StringUtil;
import com.jpyl.jpMedicalTreatment.view.activity.BaseActivity;

import butterknife.BindView;

import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by 15697 on 2019/4/3.
 */

public class SecurityCheckActivity extends BaseActivity {
    @BindView(R.id.et_feedBack_content)
    EditText etFeedBackContent;
    @BindView(R.id.tv_feedBack_length)
    TextView tvFeedBackLength;
    RxSpf_UserModel spfUserModel;
    final RequestApi publicApi = Network.getRequestApi();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_securitycheck;
    }

    @Override
    public void afterCreate(Bundle savedInstanceState) {
        mTitleTv.setText(R.string.mine_securityCheck);
        StringUtil.setEtLength(tvFeedBackLength, etFeedBackContent);
        spfUserModel = RxSpf_UserModel.create(this);
    }

    @OnClick(R.id.btn_feedBack_submit)
    public void onViewClicked() {
        String content = etFeedBackContent.getText().toString();
        if (StringUtil.isEmpty(content)) {
            etFeedBackContent.requestFocus();
            ViewKit.showToast(SecurityCheckActivity.this, "请输入防伪码");
            return;
        }
        jpylValidateProduct(content);
    }

    //查询真伪
    private void jpylValidateProduct(String traceid) {

        publicApi.jpylValidateProduct(traceid).map(new Func1<ResponseModel<String>, String>() {
            @Override
            public String call(ResponseModel<String> responseModel) {
                if (responseModel.getCode() != 0) {
                    ErrorMessageModel messageModel = new ErrorMessageModel(responseModel.getMsg());
                    Log.e("Activity","====responseModel.getMsg()======"+responseModel.getMsg());
                    throw new ApiError(messageModel);
                }
                return responseModel.getMsg();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        ViewKit.showToast(SecurityCheckActivity.this,"查询失败");
                    }

                    @Override
                    public void onError(Throwable e) {
//                        e.printStackTrace();
                        if (e instanceof ApiError) {
                            ApiError error = (ApiError) e;
                            ViewKit.showToast(SecurityCheckActivity.this,error.getMessageModel().getMsg());
                        } else {
                            ViewKit.showToast(SecurityCheckActivity.this,"查询失败");
                        }
                    }

                    @Override
                    public void onNext(String msg) {
                        ViewKit.showToast(SecurityCheckActivity.this,msg);
                    }
                });
    }
}
